<%@ page import="com.vo.Students" %>
<%@ page import="com.factory.DAOFactory" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>更新学生信息</title>
</head>
<body>
<%
    // 设置请求编码
    request.setCharacterEncoding("UTF-8");

    // 获取表单提交的数据
    String studentId = request.getParameter("studentId");
    String studentName = request.getParameter("studentName");
    String studentPwd = request.getParameter("studentPwd");
    String gender = request.getParameter("gender");
    int classId = Integer.parseInt(request.getParameter("classId"));

    // 封装到 Students 对象中
    Students stu = new Students();
    stu.setStudentId(studentId);
    stu.setStudentName(studentName);
    stu.setStudentPwd(studentPwd);
    stu.setGender(gender);
    stu.setClassId(classId);

    boolean isSuccess = false;
    try {
        // 调用 DAO 更新学生信息
        isSuccess = DAOFactory.getStudentsDAOInstance().updateStudents(stu);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // 根据更新结果跳转页面
    if (isSuccess) {
        out.println("<script>alert('学生信息更新成功！'); window.location.href='showStudents.jsp';</script>");
    } else {
        out.println("<script>alert('学生信息更新失败！'); window.location.href='updateStudents.jsp?studentId=" + studentId + "';</script>");
    }
%>
</body>
</html>